package com.artezio.task.services.impl;

import com.artezio.task.dao.ContactTelDetailDao;
import com.artezio.task.dao.impl.ContactsDaoImpl;
import com.artezio.task.model.Contact;
import com.artezio.task.model.ContactTelDetail;
import com.artezio.task.services.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;

@Transactional
public class ContactsServiceImpl implements ContactsService {

    @Autowired
    private ContactsDaoImpl contactsDAO;

    @Autowired
    private ContactTelDetailDao contactTelDetailDao;

    @Transactional
    public void addNewContact(Contact contact) {
        incVersion(contact);
        contactsDAO.addNewContact(contact);
    }

    @Transactional
    @Override
    public void saveContact(Contact contact) {
        contactsDAO.saveContact(contact);
    }

    @Override
    public void incVersion(Contact contact) {
        contact.setVersion(contact.getVersion() + 1);
    }

    @Override
    public Contact createRandomContact() {
        Random rnd = new Random();
        Contact contact = new Contact();
        contact.setVersion(rnd.nextInt(1000));
        //Date date = new Date();
        // date.setTime(rnd.nextLong());
        // contact.setBirthDate(date);
        contact.setFirstName("FirstName " + rnd.nextInt(100));
        contact.setLastName("LastName " + rnd.nextInt(100));
        return contact;
    }

    @Transactional
    @Override
    public List<Contact> getAllContacts() {
        return contactsDAO.getAllContacts();
    }

    @Transactional
    @Override
    public Contact findContactById(int id) {
        return contactsDAO.findContactById(id);
    }

    @Transactional
    @Override
    public void deleteContactById(int id) {
        contactsDAO.deleteContactById(id);
    }

    @Transactional
    @Override
    public void deleteContact(Contact contact) {
        contactsDAO.deleteContact(contact);
    }


    @Override
    public void addNewContactTelDetail(Contact contact, ContactTelDetail contactTelDetail) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transactional
    @Override
    public void saveContactTelDetail(ContactTelDetail contactTelDetail) {
        contactTelDetailDao.saveContactTelDetail(contactTelDetail);
    }

    @Transactional
    @Override
    public List<ContactTelDetail> getAllContactTelDetails() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transactional
    @Override
    public ContactTelDetail findContactTelDetailById(int id) {
        return contactTelDetailDao.findContactTelDetailById(id);
    }

    @Transactional
    @Override
    public void deleteContactTelDetailById(int id) {
        contactTelDetailDao.deleteContactTelDetailById(id);
    }

    @Transactional
    @Override
    public void deleteContactTelDetail(ContactTelDetail contactTelDetail) {
        contactTelDetailDao.deleteContactTelDetail(contactTelDetail);
    }

    @Override
    public List<ContactTelDetail> getContactsTelDetailByCriteria() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Contact> getContactsByCriteriaSortByNameAjax(String firstName, String lastName, String telType, String telNumber, String order) {
        return contactsDAO.getContactsByCriteriaSortByNameAjax(firstName, lastName, telType, telNumber, order);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Contact> getContactsByCriteriaSortByLastNameAjax(String firstName, String lastName, String telType, String telNumber, String order) {
        return contactsDAO.getContactsByCriteriaSortByLastNameAjax(firstName, lastName, telType, telNumber, order);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Contact> getContactsByCriteriaPaginAjax(String firstName, String lastName, int maxRes,int firstRes, String order, String sSearch) throws UnsupportedEncodingException {
        return contactsDAO.getContactsByCriteriaPaginAjax(firstName, lastName, maxRes, firstRes, order, sSearch);
    }

    @Override
    public List<Contact> getContactsByCriteria(String firstName, String lastName, String telType, String telNumber) {
       List<Contact> contacts = contactsDAO.getContactsByCriteria(firstName, lastName, telType, telNumber);
        return contacts;  //To change body of implemented methods use File | Settings | File Templates.
    }

}
